package org.gem.example.sale;

import org.gem.example.strategy.DiscountStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
    private final List<ShoppingItem> items = new ArrayList<>();
    private DiscountStrategy discountStrategy;

    public void addItem(ShoppingItem item) {
        items.add(item);
    }

    public BigDecimal getTotalPrice() {

        BigDecimal result = items.stream()
                .map(ShoppingItem::getTotalPrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        return (discountStrategy != null ? discountStrategy.applyDiscount(result) : result).setScale(2, RoundingMode.HALF_UP);
    }

    public List<ShoppingItem> getItems() {
        return new ArrayList<>(items);
    }

    public void setDiscountStrategy(DiscountStrategy discountStrategy) {
        this.discountStrategy = discountStrategy;
    }
}
